package com.yunshu.yunshuapp.xianhuo;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.Image;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yunshu.yunshuapp.HomeListAdapter;
import com.yunshu.yunshuapp.OnTapListener;
import com.yunshu.yunshuapp.R;
import com.yunshu.yunshuapp.WebviewWithShareActivity;
import com.yunshu.yunshuapp.network.YSCallback;
import com.yunshu.yunshuapp.network.YSRequest;
import com.yunshu.yunshuapp.utils.YSUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhouwenjie on 2017/1/27.
 */
public class XianhuoListAdapter extends RecyclerView.Adapter implements OnTapListener {

    private final ImageLoader mImageLoader;
    private final DisplayImageOptions options;
    private ArrayList<Xianhuo> models;
    public OnTapListener tapListener;
    public Activity activity;
    public XianhuoClickListener onClickListener;

    public Boolean shouldShowAudit = false;

    public XianhuoListAdapter(Context context){
        // 使用ImageLoader之前初始化
        YSUtils.initImageLoader(context);

        models = new ArrayList<>();
        // 获取图片加载实例
        mImageLoader = ImageLoader.getInstance();
        options = YSUtils.displayImageOptions();

        tapListener = this;
    }

    @Override
    public int getItemCount() {

        int count = models.size();
        return count ;
    }

    @Override
    public int getItemViewType(int position) {
        Xianhuo m = models.get(position);
        return Integer.parseInt(m.getType()) ;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.xianhuo_list_item,parent,false);
//        ListViewHolder holder = new ListViewHolder(view);
//        return holder;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.xianhuo_list_item2,parent,false);
        HomeListAdapter.ListViewHolder holder = new HomeListAdapter.ListViewHolder(view);
        holder.tapListener = this;
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

//        if (position >= models.size()) return;
//        Xianhuo model = models.get(position);
//        ListViewHolder xhHolder = (ListViewHolder) holder;
//        xhHolder.setPosition(position);
//        xhHolder.categoryTV.setText(model.getChannelname());
//        mImageLoader.displayImage(model.getKucunpic(), xhHolder.imgView);
//        xhHolder.dateTV.setText(model.getCreatetime());
//        xhHolder.locationTV.setText(model.getPlace());
//
//        if (model.getProductname().length()>0) {
//            xhHolder.categoryTV.setText(model.getChannelname() + "(" + model.getProductname() + ")");
//        }else {
//            xhHolder.categoryTV.setText(model.getChannelname() );
//        }
//        xhHolder.dateTV.setText(model.getCreatetime());
//
//        xhHolder.companyTV.setText("公司: "+model.getCompany());
//        xhHolder.factoryTV.setText("生产厂家: "+model.getFactory());
//
//
//        SpannableStringBuilder style = createPriceStyle("价格: "+model.getPrice());
//        xhHolder.priceTV.setText(style);
//
//        xhHolder.specificationTV.setText("规格: "+model.getSpecification());
//        xhHolder.qualityTV.setText("材质: "+model.getQuality());

        Xianhuo model = models.get(position );
        HomeListAdapter.ListViewHolder xhHolder = (HomeListAdapter.ListViewHolder) holder;
        xhHolder.setPosition(position);
        xhHolder.categoryTV.setText(model.getChannelname());
        mImageLoader.displayImage(model.getKucunpic(), xhHolder.imgView);
        xhHolder.dateTV.setText(model.getCreatetime());
        xhHolder.locationTV.setText(model.getPlace());

        if (model.getProductname().length()>0) {
            xhHolder.categoryTV.setText(model.getChannelname() + "(" + model.getProductname() + ")");
        }else {
            xhHolder.categoryTV.setText(model.getChannelname() );
        }
        xhHolder.dateTV.setText(model.getCreatetime());
        xhHolder.titleTV.setText(model.getAdvert());
        xhHolder.companyTV.setText("公司: "+model.getCompany());

        if (this.shouldShowAudit){
            xhHolder.setAuditDes(model.getAudit());
        }
        return;

//        if (model.getType() == Xianhuo.RecyclerViewItemTypeXianhuo){
//
//        }else if (model.getType() == Xianhuo.RecyclerViewItemTypeCaigou) {
//
//        }

    }

    private SpannableStringBuilder createPriceStyle(String price){

        int startIndex=-1 , endIndex = price.length() - 1;

        for (int i = 0 ; i < price.length() ; i++ ){
            Character c = price.charAt(i);
            if (c >= '0' && c <= '9'){
                if (startIndex < 0 ){
                    startIndex = i;
                }
            }else {
                if (startIndex >= 0 ){
                    endIndex = i;
                    break;
                }
            }
        }

        SpannableStringBuilder style=new SpannableStringBuilder(price);
        if (startIndex > 0) {
            style.setSpan(new ForegroundColorSpan(YSUtils.getColor(R.color.title_gray2,this.activity)),0,startIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色
            style.setSpan(new RelativeSizeSpan(0.8f),0,startIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色
        }

        if (endIndex < price.length() - 1){
            style.setSpan(new ForegroundColorSpan(YSUtils.getColor(R.color.title_gray2,this.activity)),endIndex,price.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色
            style.setSpan(new RelativeSizeSpan(0.8f),endIndex,price.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色

        }

        return style;
    }

    @Override
    public void onSingleTap(View v, int position) {
        Xianhuo model = models.get(position);

        if (this.onClickListener != null){
            this.onClickListener.onClick(model);
            return;
        }
        Intent intent = new Intent();
        intent.setClass(this.activity, XianhuoDetailActivity2.class);
        Bundle bundle = new Bundle();

        String url = "http://www.yunshutech.com/order.action#offer?fromApp=1&id=" + model.getXianhuoId();
        bundle.putString("url",url);
        bundle.putString("title","现货详情");
        bundle.putString("xianhuo",model.serializeToString());
        intent.putExtras(bundle);
        this.activity.startActivityForResult(intent,0);
    }

    class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView locationImg;
        TextView categoryTV;
        ImageView imgView;
        TextView dateTV;
        TextView locationTV;
//        View companyLayout;
        TextView companyTV;
        TextView factoryTV;
//        TextView priceTV;
//        TextView specificationTV;
//        TextView qualityTV;
        int mPosition;

        public ListViewHolder(View itemView ) {
            super(itemView);
            itemView.setOnClickListener(this);

            imgView = (ImageView) itemView.findViewById(R.id.imageView);
            factoryTV = (TextView) itemView.findViewById(R.id.factoryTV);
            categoryTV = (TextView)itemView.findViewById(R.id.categoryTV);
            dateTV = (TextView) itemView.findViewById(R.id.dateTV);
            locationTV = (TextView) itemView.findViewById(R.id.locTV);
            locationImg = (ImageView)itemView.findViewById(R.id.locImg);
//            priceTV = (TextView)itemView.findViewById(R.id.priceTV);
            companyTV = (TextView) itemView.findViewById(R.id.companyTV);
//            specificationTV = (TextView)itemView.findViewById(R.id.specificationTV);
//            qualityTV = (TextView)itemView.findViewById(R.id.qualityTV);
        }

        public void setPosition(int position) {
            mPosition = position;
        }

        @Override
        public void onClick(View view) {
            tapListener.onSingleTap(view,mPosition);
        }

        public void setAuditDes(String audit) {
            if ("0".equalsIgnoreCase(audit)){
                locationTV.setText("待审核");
            }else if ("1".equalsIgnoreCase(audit)){
                locationTV.setText("审核通过");
            }else if ("2".equalsIgnoreCase(audit)){
                locationTV.setText("审核不通过");
            }
            locationTV.setTextColor(Color.RED);
            locationImg.setVisibility(View.INVISIBLE);

        }
    }

    public void setModels(List<Xianhuo> newModels){
        models.clear();
        models.addAll(newModels);
    }

    public void addFooterItem(List<Xianhuo> footerDatas) {
        models.addAll(footerDatas);
        notifyDataSetChanged();
    }
}
